<template>
  <div>
    <div class="chart" v-resize="resizeCharts" ref="barChart" v-if="data.length"></div>
    <el-empty v-else description="暂无数据" class="chart" ></el-empty>
  </div>
</template>
<script>
import * as echarts from "echarts";
export default {
  name: "barChart",
  props: {
    barBorderRadius: {
      type: Array,
      default() {
        return [0, 0, 0 , 0]
      }
    },
    isxAxis: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Array,
      default() {
        return [];
      },
    },
    xAxis: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      charts: null,
      defaultColor: ['#fc8500', '#f8ba3d', '#29cc9c', '#4ed1f8']
    };
  },
  methods: {
    resizeCharts() {
      this.charts.resize();
    },
    initCharts() {
      this.$nextTick(() => {
        this.charts = echarts.init(this.$refs.barChart);
        this.charts.clear();
        this.setOption();
      });
    },
    setOption() {
      const option = this.getOption();
      this.charts.setOption(option, true);
    },
    getOption() {
      const option = {
        grid: {
        },
        dataZoom: !this.isxAxis ? [{
          type: 'slider',
          start: 0,
          show: this.data[0].data.length > 5,
          backgroundColor: "#fff",
          fillerColor: "#bbb",
          borderColor: "#ddd",
          height: "5",
          endValue: 4,
          textStyle: false,
          zoomLock: true,
        }] : [{
      type: 'slider',
      show: true,
      yAxisIndex: 0,
      right: 40,    
      top: 0,
      start: 0,
      endValue: 3,
      width: 5,
      backgroundColor: "#fff",
      fillerColor: "#bbb",
      borderColor: "#ddd",
    },
    {
      type: 'inside',
      yAxisIndex: [0],
      start: 29,
      end: 36,
      // 不按任何功能键，鼠标滚轮能触发缩放
      zoomOnMouseWheel: false,
      // 不按任何功能键，鼠标移动能触发数据窗口平移
      moveOnMouseWheel: true
    }],
        color: this.data.map((item, i) => {
          const index = i % 4
          const color = item.color || this.defaultColor[index]
          return color
        }),
        tooltip: {
          show: true,
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        legend: {
          bottom: "10",
        },
        xAxis: !this.isxAxis ? {
          type: "category",
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            interval: 0,
            rotate: 0,
          },
          data: this.xAxis,
        } : {
          type: "value",
        },
        yAxis: !this.isxAxis ? {
          type: "value",
          name: "单位（万元）",
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
        } : {
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          type: "category",
          name: "年份",
          data: this.xAxis,
        },
        series: this.data.map((item) => {
          return {
            name: item.name,
            data: item.data,
            barGap: "100%",
            type: "bar",
            itemStyle: {
              normal: {
                barBorderRadius: this.barBorderRadius
              }
            },
            barWidth: 20,
          };
        }),
      };
      return option;
    },
  },
  mounted() {
    this.data.length && this.initCharts();
  },
  beforeDestroy() {
    this.charts && this.charts.dispose();
    this.charts = null;
  },
  watch: {
    data: {
      handler() {
        if (this.charts) {
          this.setOption();
        } else {
          this.initCharts();
        }
      },
      deep: true,
    },
    xAxis: {
      handler() {
        if (this.charts) {
          this.setOption();
        } else {
          this.initCharts();
        }
      },
      deep: true,
    },
  },
};
</script>

<style scoped lang="less">
.chart {
  width: 100%;
  height: 100%;
}
</style>
